<?php
require_once "../config.php";
error_reporting(1);
$country = new Country();
$countries=$country->getCoutryList();
$cities=$country->getCityList();
//print_r($cities);
$hotel=new Hotels();
$hotel_details_arr=array();
$mode=$_GET['mode'];
$id=$_GET['id'];
$title="Add";
if (!empty($mode) && !empty($id)){
    switch (strtolower($mode)) {
        case 'e':
            $title="Edit";
            $edit_arr=$hotel->get_hotels($id);
            foreach ($edit_arr as $key => $val) {
                $hotel_details_arr=$val;                
            }
            break;
        
        case 'r':
            $hotel->remove_hotel($id);
            $err_msg="Removed successfully";            
            break;
        
        default:
            break;
    }
}

if(isset($_POST['save'])){    
    $validation_obj = new Validation();   
    
    $validation_error=array();
    $hotel_arr=array();
    //exit('ss');
    $hotel_arr=$_POST;
    //print_r($hotel_arr);
    foreach ($hotel_arr as $key => $value) {
        if($validation_obj->check_blank(trim($value) == true)) {
            $validation_error[$key] = "<span class='error'>$key is blank</span>";
        }
    }
    unset($validation_error['hotel_id']);
    //print_r($validation_error);
    if(empty($validation_error)){
        $hotel_details_arr=array(
            "hotel_name"=>$hotel_arr['hotel_name']
            ,"country_id"=>$hotel_arr['country_id']
            ,"city_id"=>$hotel_arr['city_id']
            ,"hotel_address"=>$hotel_arr['hotel_address']
            ,"hotel_description"=>$hotel_arr['hotel_description']
            ,"contact_number"=>$hotel_arr['contact_number']
            ,"terms_and_conditions"=>$hotel_arr['terms_and_conditions']
            ,"hotel_code"=>$hotel_arr['hotel_code']
        );
        //echo "hotel_id: ".$hotel_arr['hotel_id'];
        if(!empty($hotel_arr['hotel_id'])){
            $hotel->update_hotel($hotel_details_arr, $hotel_arr['hotel_id']);
            $hotel_details_arr['hotel_id']=$hotel_arr['hotel_id'];
            $err_msg="Successfully updated";
        }else{
            $hotel_details_arr['hotel_id']=$hotel->insert_hotel($hotel_details_arr);
            $err_msg="Successfully added";
        }
    }
}

$hotel_list=array();
$hotel_list=$hotel->get_hotels();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Admin</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<script language="Javascript" type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>
<style media="all" type="text/css">@import "css/all.css";</style>
<script language="Javascript" type="text/javascript" src="js/popup.js"></script>
<link href="../css/general.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="main">
        <?php include 'header.php'; ?>
	<div id="middle">
            <?php include_once 'left_panel.php'; ?>
            <div id="center-column">
                <div class="top-bar">
                    <h1><?php echo $title;?> New Hotel</h1>
                </div>
                <div class="clear"></div>
                <?php if(!empty($err_msg)):?>
                <div class="top-bar">
                    <h3><?php echo $err_msg; ?></h3>
                </div>
                <div class="clear"></div>
                <?php endif;?>
                <div class="table">
                    <table class="listing" cellpadding="0" cellspacing="0">
                        <form name="add_new_offer" id="add_new_offer" method="post" action="hotels.php" enctype="m">
                            <input type="hidden" name="hotel_id" id="hotel_id" value="<?php echo (!empty($hotel_details_arr['hotel_id']))?$hotel_details_arr['hotel_id']:""?>" />
                            <tr>
                                <td>Hotel Name</td>
                                <td align="left"> <input type="text"  value="<?php echo isset($_POST['hotel_name'])?$_POST['hotel_name']:$hotel_details_arr['hotel_name']?>" style="width:350px" name="hotel_name" id="hotel_name" /> </td>
                            </tr>
                            <tr>
                                <td>Country</td>
                                <td align="left"> 
                                    <select id="country_id" name="country_id">
                                        <?php foreach ($countries as $key => $val): ?>
                                        <option value="<?php echo $val['country_id']?>" <?php echo (($val['country_id']==$_POST['country_id']) || ($val['country_id']==$hotel_details_arr['country_id']))?"selected":"";?>><?echo $val['country_name']?></option>
                                        <?php endforeach; ?>
                                    </select> 
                                </td>
                            </tr>
                            <tr>
                                <td>City</td>
                                <td align="left"> 
                                    <select id="city_id" name="city_id">
                                        <?php foreach ($cities as $key => $val):?>
                                        <option value="<?php echo trim($val['city_id']);?>" <?php echo  ($val['city_id']==$_POST['city_id']) || ($val['city_id']==$hotel_details_arr['city_id'])?"selected":"";?>><?echo $val['city_name']?></option>
                                        <?php endforeach; ?>
                                    </select> 
                                </td>
                            </tr>
                            <tr>
                                <td>Hotel Address</td>
                                <td align="left"> <textarea name="hotel_address" id="hotel_address" style="width: 350px;">
                                    <?php echo isset($_POST['hotel_address'])?trim($_POST['hotel_address']):trim($hotel_details_arr['hotel_address'])?>
                                    </textarea> </td>
                            </tr>
                            <tr>
                                <td>Description</td>
                                <td align="left"> <textarea name="hotel_description" id="hotel_description" style="width: 350px;">
                                    <?php echo isset($_POST['hotel_description'])?$_POST['hotel_description']:$hotel_details_arr['hotel_description']?>
                                    </textarea> </td>
                            </tr>
                            <tr>
                                <td>Contact No.</td>
                                <td align="left"> <input type="text" value="<?php echo isset($_POST['contact_number'])?$_POST['contact_number']:$hotel_details_arr['contact_number']?>" style="width:350px" name="contact_number" id="contact_number" style="width:350px" /></td>
                            </tr>
                            <tr>
                                <td>Terms & Conditions</td>
                                <td align="left"> <textarea name="terms_and_conditions" id="terms_and_conditions"  style="width: 350px;">
                                    <?php echo isset($_POST['terms_and_conditions'])?$_POST['terms_and_conditions']:$hotel_details_arr['terms_and_conditions']?>
                                    </textarea> </td>
                            </tr>
                            <tr>
                                <td>Hotel Code</td>
                                <td align="left" > <input type="text" value="<?php echo isset($_POST['hotel_code'])?$_POST['hotel_code']:$hotel_details_arr['hotel_code']?>" style="width:350px" name="hotel_code" id="hotel_code" /> </td>
                            </tr>
                            <?php if (!empty($hotel_details_arr['hotel_id'])):?>
                            <tr>
                                <td></td>
                                <td align="left" > <a href="#" class="upload_img" id="upload_img">Upload Images</a></td>
                            </tr>
                            <?php endif;?>
                            <tr>
                                <td colspan="2"><input type="submit" name="save" id="save" value="Save"></td>
                                
                            </tr>
                        </form>
                    </table>

                </div>

            </div>
            <br /><br />
            <div class="clear"></div>
            <?php if (!empty($hotel_list)): ?>
                <div class="list">
                    <table class="title"><tr>
                        <td>Hotel Name</div>
                        <td>Country</td>
                        <td>City</td>
                        <td>Hotel Address</td>
                        <td>Description</td>
                        <td>Contact No.</td>
                        <td>Terms & Conditions</td>
                        <td>Hotel Code</td>
                        <td colspan="2">Manage</td></tr>

                    <?php foreach ($hotel_list as $key => $val) :
                        $city_name=$country->getCityList('city_name', 'city_id='.$val['city_id']);
                        $country_name=$country->getCoutryList('country_name', 'country_id='.$val['country_id']);
                        //print_r($city_name);
                        ?>

                        <td><?php echo $val['hotel_name'];?></td>
                        <td><?php echo ($country_name[0]['country_name'])?$country_name[0]['country_name']:'-';?></td>
                        <td><?php echo ($city_name[0]['city_name'])?$city_name[0]['city_name']:'-';?></td>
                        <td><?php echo $val['hotel_address'];?></td>
                        <td><?php echo $val['hotel_description'];?></td>
                        <td><?php echo $val['contact_number'];?></td>
                        <td><?php echo $val['terms_and_conditions'];?></td>
                        <td><?php echo $val['hotel_code'];?></td>
                        <td><a href="hotels.php?mode=e&id=<?php echo $val['hotel_id']?>">Edit</a></td>
                        <td><a href="hotels.php?mode=r&id=<?php echo $val['hotel_id']?>" onclick="return doublecheck();">Delete</a></td></tr>

                    <?php endforeach; ?>
                </div>
            <?php endif;?>		
	</div>
	<div id="footer"> All rights reserved</div><div class="clear"></div>
</div>


<div id="popupContact">
    <a id="popupContactClose">x</a>
    
    <!-- Toggle and show this on success -->
    <div style="display:none" id="err_msg">
    &nbsp;&nbsp;&nbsp;&nbsp;<img src="" id="f1" />  &nbsp;&nbsp;&nbsp;&nbsp;<img src="" id="f2"  />
    </div>    
    
    <div id="img_upload">
        <form method="post" action="image_uploader.php?oid=<?php echo !empty($hotel_details_arr['hotel_id'])?$hotel_details_arr['hotel_id']:0; ?>" enctype="multipart/form-data" name="img_upload_form" id="img_upload_form">
            <div>Picture : </div>
            <div><input size="25" name="file" type="file" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10pt" class="box"/></div>			  
            <div>Image maximum size <b>400 </b>kb</div>
            <div><input type="button" id="mybut" value="       Upload        " name="mybut"/></div>
        </form>
        <div id='preview'></div>            
    </div>   
    <div id="Loader" style="display:none"><img src="images/loading.gif" /></div>
</div>
<div id="backgroundPopup"></div>
</body>
    <script type="text/javascript">
        function doublecheck(){
            if (confirm("Are you sure you want to delete this record?")){
                return true;
            }
            return false;
        }
    </script>
</html>
